package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.pojo.Record;
import com.shopping.service.record.RecordServiceImpl;
import com.shopping.util.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RecordController {

    @Autowired
    RecordServiceImpl recordService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = {"/record/scanTrade"})
    public Map<String, Object> scanTrade(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Record record = new Record();
        record.setTradeId(Integer.parseInt(jsonObject.getString("tradeId")));
        record.setTime(jsonObject.getDate("time"));
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(record)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
            return map;
        }
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        record.setUserId(redisUserId);
        return recordService.addRecord(record);
    }

    @PostMapping(value = {"/record/showAllRecords"})
    public Map<String, Object> showAllRecords(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return recordService.showRecords(redisUserId);
    }

    @PostMapping(value = {"/record/deleteRecord"})
    public Map<String, Object> deleteRecord(@RequestBody JSONObject jsonObject){
        int id = Integer.parseInt(jsonObject.getString("id"));
        return recordService.deleteRecord(id);
    }

    @PostMapping(value = {"/record/deleteAllRecords"})
    public Map<String, Object> deleteAllRecords(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return recordService.deleteAllRecords(redisUserId);
    }

}
